// pages/my/replaceProduct/replaceSet.js
// 133,134 
var app = getApp()
Page({
  data: {
    ids:'',
    oldIdsStr:'',
    goods:[],
    oldIds:[],
    oldGoods:[],
    goodsPriceShow:true,
    actualPriceShow:true,
    goodsInfoShow:true,
    isMask:false,
    isScrap:false,
    scrapPrice:'',
    nowScrapId:''
  },
  onLoad: function (options) {
    // let ids = 14
    let ids = options.ids
    this.setData({
      ids
    })
    app.getMemInfo().then(res=>{
      this.setData({
        accountId:res.data.id
      })
      this.getNewGoodsList(res.data.id,ids)
    })
    console.log(options.ids)
    
  },
  onReady: function () {

  },
  onShow: function () {
    app.getMemInfo().then(res=>{
      this.getOldGoodList(res.data.id)
    })
    this.setData({
      isMask:false
    })
  },
  getNewGoodsList(accountId,ids){
    let that = this
    let data = {
      accountId,
      ids
    }
    console.log(data)
    app.request('wx_app/oldForNew/cart/skuListByIds',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let goods = []
        res.data.carts.forEach(item=>{
          goods.push({
            url:item.thumbUrl,
            name:item.productName,
            price:item.price,
            actualPrice:item.actualPrice,
            num:item.count,
            type:item.versionValues,
            companyName:item.productTag,
            color:item.tagColor
          })
        })
        that.setData({
          goods
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  getOldGoodList(accountId){
    let ids = 0
    this.data.oldIds.forEach(item=>{
      ids+=item+','
    })
    ids=ids.slice(0,ids.length-1)
    this.setData({
      oldIdsStr:ids
    })
    let data = {
      accountId,
      ids,
    }
    console.log(data)
    app.request('wx_app/oldForNew/oldSku/getByIds',data,'GET').then(res=>{
      console.log(res)
      let oldGoods = []
      if (res.code==1) {
        res.data.forEach(item=>{
          oldGoods.push({
            url:item.mainImgUrl,
            name:item.productName,
            price:item.stockPrice,
            // actualPrice:80,
            num:item.amount,
            type:item.versionValues,
            imgs:[item.imgUrl2,item.imgUrl3,item.imgUrl4,item.imgUrl5],
            id:item.id,
            color:item.tagColor,
            companyName:item.productTag,
            choose:2,
            scrapPrice:item.discardPrice==null?item.stockPrice:item.discardPrice
          })
        })
        this.setData({
          oldGoods
        })
      }else{
        // app.errorMsg(res.message)
      }
    })
  },
  addOldGood(){
    wx.navigateTo({
      url: 'addOldGood'
    })
  },
  //删除旧商品
  del(e){
    let data = {
      id:e.currentTarget.dataset.id
    }
    let i = e.currentTarget.dataset.index
    console.log(data)
    app.request('wx_app/oldForNew/oldSku/del',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        app.errorMsg('删除成功')
        let oldIds = this.data.oldIds
        oldIds.splice(i,1)
        this.setData({
          oldIds
        })
        this.getOldGoodList(this.data.accountId)
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //生成订单去支付
  next(){
    const [newPrice,oldPrice] = [0,0]
    this.data.oldGoods.forEach(item=>{
      if (item.choose==2) {
        app.errorMsg('请选择商品去向')
        throw SyntaxError()
      }else{

      }
    })
    if (this.data.goods.length==0) {
      app.errorMsg('没有添加被兑换的新商品')
    }else {  

      let data = {
        skuIds:this.data.ids,
        accountId:this.data.accountId,
        oldSkuIds:this.data.oldIdsStr
      }
      console.log(data)
      app.request('wx_app/oldForNew/add',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {
           wx.redirectTo({
            url: 'replacePay?orderId='+res.data.id+'&type=0'
          })
        }else{
          app.errorMsg(res.message)
        }
      })
    }
   
  },
  choiceAddMethod(){
    this.setData({
      isMask:true
    })
  },
  cancelAdd(){
    this.setData({
      isMask:false
    })
  },
  toOldGoodsList(){
    wx.navigateTo({
      url:'oldGoodsList'
    })
  },
  //入库
  putInStock(e){
    let that = this
    let id = e.currentTarget.dataset.id
    let data = {
      id,
      complete:1,
      status:'',
      discardPrice:''
    }
    app.request('wx_app/oldForNew/oldSku/discarded',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        that.data.oldGoods.forEach((item,index)=>{
          if (item.id==id) {
            that.setData({
              ['oldGoods['+index+'].choose']:0
            })
          }
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //报废
  bindScrap(e){
    console.log(e.currentTarget.dataset.index)
    this.setData({
      isScrap:true,
      scrapPrice:e.currentTarget.dataset.price,
      nowScrapId:e.currentTarget.dataset.id
    })
  },
  //报废弹窗
  scrapCancel(){
    this.setData({
      isScrap:false
    })
  },
  scrapConfirm(){
    let that = this
    let data = {
      id:this.data.nowScrapId,
      complete:'',
      status:1,
      discardPrice:this.data.scrapPrice
    }
    console.log(data)
    app.request('wx_app/oldForNew/oldSku/discarded',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        that.setData({
          isScrap:false
        })
        that.data.oldGoods.forEach((item,index)=>{
          if (item.id==that.data.nowScrapId) {
            that.setData({
              ['oldGoods['+index+'].scrapPrice']:this.data.scrapPrice,
              ['oldGoods['+index+'].choose']:1
            })
          }
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  getScrapPrice(e){
    this.setData({
      scrapPrice:e.detail.value
    })
  }
})